﻿using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspDotnetPractice.CustomServerControl.Subproperty;

[assembly: TagPrefix("AspDotnetPractice.CustomServerControl", "practiceUI")]
namespace AspDotnetPractice.CustomServerControl
{
    [AspNetHostingPermission(SecurityAction.Demand,Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [DefaultProperty("Title")]
    [ToolboxData("<{0}:Book runat=\"server\"></{0}:Book>")]
    public class Book : WebControl
    {
        private Author author;
        private string initialAuthorString;

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Description("The author of the book.")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public Author Author
        {
            get { return author ?? (author = new Author()); }
            set { author = value; }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Description("The title of the book.")]
        [Localizable(true)]
        public virtual string Title
        {
            get 
            { 
                var title = (string) ViewState["Title"];
                return title ?? string.Empty;
            }
            set { ViewState["Title"] = value; }
        }

        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue("0.00"),
        Description("The price of the book."),
        Localizable(true)
        ]
        public virtual decimal Price
        {
            get
            {
                object price = ViewState["Price"];
                return (price == null) ? decimal.Zero : (decimal)price;
            }
            set
            {
                ViewState["Price"] = value;
            }
        }

        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("The symbol for the currency."),
        Localizable(true)
        ]
        public virtual string CurrencySymbol
        {
            get
            {
                string s = (string)ViewState["CurrencySymbol"];
                return s ?? string.Empty;
            }
            set
            {
                ViewState["CurrencySymbol"] = value;
            }
        }

        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue(BookType.NotDefined),
        Description("Fiction or Not"),
        ]
        public virtual BookType BookType
        {
            get
            {
                object t = ViewState["BookType"];
                return (t == null) ? BookType.NotDefined : (BookType)t;
            }
            set
            {
                ViewState["BookType"] = value;
            }
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);

            Author authorValue = (Author) ViewState["Author"];
            if (authorValue != null)
            {
                author = authorValue;
            }
        }

        protected override object SaveViewState()
        {
            if (author != null)
            {
                string authorString = author.ToString();
                if (!authorString.Equals(initialAuthorString))
                {
                    ViewState["Author"] = authorString;
                }
            }
            return base.SaveViewState();
        }

        protected override void TrackViewState()
        {
            if (author != null)
            {
                initialAuthorString = author.ToString();
            }
            base.TrackViewState();
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.WriteEncodedText(Title);
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.WriteEncodedText(Author.ToString());
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.WriteEncodedText(BookType.ToString());
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(CurrencySymbol);
            writer.Write("&nbsp;");
            writer.Write(string.Format("{0:F2}", Price));
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderEndTag();
        }
    }
}
